package com.sms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.sms.businessservice.DemoBusinessService;
import com.sms.vo.DemoVO;

public class DemoController  extends MultiActionController{
	private static final Logger LOG = Logger.getLogger(DemoController.class);

	private BindingResult errors = null;
	private DemoBusinessService demoBusinessService;

	public void setDemoBusinessService(DemoBusinessService mcMembersBusinessService) {
		this.demoBusinessService = mcMembersBusinessService;
	}

	protected void bind(HttpServletRequest request, Object command) {
		final ServletRequestDataBinder binder = new ServletRequestDataBinder(command);
		binder.registerCustomEditor(String.class,new StringTrimmerEditor(false));
		binder.bind(request);
		errors = binder.getBindingResult();
	}

	public ModelAndView showDemoUI(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		final Map<String, Object> model = new HashMap<String, Object>();
		DemoVO mcMembersVO = new DemoVO();
		model.put("demoVO", mcMembersVO);
		return new ModelAndView("Demo", model);
	}

	public ModelAndView saveDemo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		final Map<String, Object> model = new HashMap<String, Object>();
		DemoVO demoVO = new DemoVO();
		bind(request, demoVO);
		if (!(errors != null && errors.getAllErrors() != null && errors
				.getAllErrors().size() > 0)) {
			demoBusinessService
					.saveDemo(demoVO);
			model.put("infoMsg", "Demo.list.updated");
		} else {
			model.put("errors", errors);
		}
		model.put("demoVO", demoVO);
		return new ModelAndView("Demo", model);

	}

}
